class Solution:
    def lastStoneWeight(self, stones) -> int:
        if len(stones)==1:
            return stones[0]
        if len(stones)==2:
            return abs(stones[1]-stones[0])
        sort_stones=sorted(stones)
        num=len(stones)
        if (sort_stones[num - 3] == 0):
            return sort_stones[num - 1] - sort_stones[num - 2]
        sort_stones[num - 1] = sort_stones[num - 1] - sort_stones[num - 2]
        sort_stones[num - 2] = 0
        return self.lastStoneWeight(sort_stones)



    def test(self):
        # stones=[1,1,4,5,1,4,1,9,1,9,8,1,0]
        stones=[2,7,4,1,8,1]
        return print(self.lastStoneWeight(stones))

if __name__ == '__main__':
    s=Solution()
    s.test()
